package test.lu.materidesign;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by Administrator on 2016/10/27 0027.
 */
public class CircleView extends View {
    private Context mContext;
    Paint paint;
    int color;
    int radius;
    private float stop = 0.7f;
    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context) {
        this(context, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = MeasureSpec.getSize(heightMeasureSpec);
        int a = Math.min(w, h);
        radius = a / 2 - 15;
        setMeasuredDimension(a, a);
    }


    private void init(Context context) {
        mContext = context;
        paint = new Paint();
        color = mContext.getResources().getColor(R.color.circle_bg);
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        ObjectAnimator oa = ObjectAnimator.ofFloat(CircleView.this, "stop", 0.7f, 0.2f, 0.7f);
        oa.setDuration(2000);
        oa.setInterpolator(new LinearInterpolator());
        oa.setRepeatCount(-1);
        oa.start();
    }



    @Override
    protected void onDraw(Canvas canvas) {
        Log.e("luchao", "onDraw: ");
        super.onDraw(canvas);
        RadialGradient tg = new RadialGradient(getMeasuredWidth() / 2, getMeasuredWidth() / 2, radius, new int[]{0xffff0000, 0xffff0000,0x00ff0000}
                , new float[]{0f, stop, 1f}, Shader.TileMode.CLAMP);
        paint.setShader(tg);
        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredWidth() / 2, radius, paint);
    }

    public void setStop(float stop){
        Log.e("luchao", "setStop: ");
        this.stop = stop;
        invalidate();
    }
}
